﻿//"TRC:100;TPC:5;PPRC:20;CPN:1;CPRC:20;CPA:Next;JMN:20;NPG:10;"
$(function event() {
    //Init
    $("#FirstButton").bind("click", { "ActionType": "First" }, Jump);
    $("#PreviousButton").bind("click", { "ActionType": "Previous" }, Jump);
    $("#NextButton").bind("click", { "ActionType": "Next" }, Jump);
    $("#LastButton").bind("click", { "ActionType": "Last" }, Jump);
    $("#QueryButton").bind("click", { "ActionType": "Jump" }, Jump);

    var titles = $("#__ListTitle").find("span");
    for (var i = 0; i < titles.length; i++) 
        $(titles[i]).bind("click", Sort);

    $("#CPN").bind("focus", SavePrevalue);
    $("#PPRC").bind("focus", SavePrevalue);
    $("#CPN").bind("blur", CheckValue);
    $("#PPRC").bind("blur", CheckValue);
    $("#CPN").bind("keypress", KeyPress);
    $("#PPRC").bind("keypress", KeyPress);

    $("#EmptyButton").click(function() {
        var eles = $(this).parents("table:first").find("[name]");
        for (var i = 0; i < eles.length; i++)
            $(eles[i]).val("");
    });
});
function Sort(event) {
    var $ele = $(event.srcElement);
    var orderby;
    if ($ele.text().indexOf("↑") > 0)
        orderby = String.format("{0} desc", $ele.attr("id"));
    else
        orderby = String.format("{0} asc", $ele.attr("id"));
    event.data = {};
    event.data["ActionType"] = "Sort";
    event.data["OrderBy"] = orderby;
    Jump(event);
}
function SetPageIndexSortInfo(pageAction, orderBy, cpn, tpc, pprc, cprc, trc) {
    $("#CPN").val(cpn);
    $("#TPC").text(tpc);
    $("#PPRC").val(pprc);
    $("#CPRC").text(cprc);
    $("#TRC").text(trc);

    if (pageAction == "Sort") {
        var titles = $("#__ListTitle").find("span");
        var field = orderBy.split(' ')[0];
        var order = orderBy.split(' ')[1];
        for (var i = 0; i < titles.length; i++) {
            var text = $(titles[i]).text();
            if (text.indexOf("↑") > 0 || text.indexOf("↓") > 0)
                $(titles[i]).text(text.substring(0, text.length - 1));
            if ($(titles[i]).attr("id") == field) {
                if (order == "asc")
                    text = "↑";
                else
                    text = "↓";
                $(titles[i]).text($(titles[i]).text() + text);
            }
        }
    }
}
function KeyPress(event) {
    if (event.keyCode == 13) {
        event.data = { "ActionType": "Jump" };
        Jump(event);
    }
}
function CheckValue(event) {
    var $pprc = $("#PPRC");
    var pprc = $pprc.val();
    if (pprc != parseInt(pprc)) {
        Alert("每页条数不为整数！");
        $pprc.val($pprc.attr("prevalue"));
        return null;
    }
    var $cpn = $("#CPN");
    var cpn = $cpn.val();
    if (cpn != parseInt(cpn)) {
        Alert("当前页数不为整数！");
        $cpn.val($cpn.attr("prevalue"));
        return null;
    }
    if (parseInt($("#TPC").text()) < parseInt(cpn)) {
        Alert("当前页数超过总页数！");
        $cpn.val($cpn.attr("prevalue"));
        return null;
    }
    var data = {};
    data["PPRC"] = pprc;
    data["CPN"] = cpn;
    return data;
}
function SavePrevalue(event) {
    $ele = $(event.srcElement);
    $ele.attr("prevalue", $ele.val());
}
function GetPageIndexSortInfo(actionType, orderBy) {
    var jsondata = CheckValue();
    if (jsondata == null)
        return;
    jsondata["ActionType"] = "Jump";
    jsondata["PageAction"] = actionType;
    if (jsondata["PageAction"] == "Sort")
        jsondata["OrderBy"] = orderBy;
    else {
        var titles = $("#__ListTitle").find("span");
        for (var i = 0; i < titles.length; i++) {
            var id = $(titles[i]).attr("id");
            if ($(titles[i]).text().indexOf("↑") > 0)
                jsondata["OrderBy"] = id + " asc";
            else if ($(titles[i]).text().indexOf("↓") > 0)
                jsondata["OrderBy"] = id + " desc";
        }
        if (!jsondata["OrderBy"])
            jsondata["OrderBy"] = "Id desc";
    }
    return jsondata;
}
function Jump(event) {
    var jsondata = GetPageIndexSortInfo(event.data.ActionType, event.data.OrderBy);

    var where = "";
    var $ele = $("[name='Body']");
    if ($.trim($ele.val()) != "")
        where += String.format("Body like '%{0}%' and ", escape($.trim($ele.val())));
    $ele = $("[name='Type']");
    if ($.trim($ele.val()) != "")
        where += String.format("Type = '{0}' and ", $.trim($ele.val()));
    if (where != "")
        where = "where " + where.substring(0, where.length - 4);
    jsondata["Where"] = where;

    ShowMask();
    $.post("QuestionList.aspx", jsondata, function(data) {
        if (data.IsSuccess == "true") {
            var eles = $("#MainTable").children().children();
            for (var i = 0; i < eles.length; i++) {
                if (i != 0 && i != 1 && i != eles.length - 1)
                    $(eles[i]).remove();
            }
            for (var i = 0; i < data.Trs.length; i++) {
                var tr = unescape(data.Trs[i]);
                $(tr).insertBefore($(eles[eles.length - 1]));
            }
            SetPageIndexSortInfo(jsondata["PageAction"], jsondata["OrderBy"], data.CPN, data.TPC, data.PPRC, data.CPRC, data.TRC);

        }
        else
            ShowError(data.Message, data.Source, data.StackTrace);
        HideMask();
    }, "json");
}       